﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 前端专用
    /// </summary>
    public partial class SoftManage
    {
        private static readonly SoftDal MySoftDAL = new SoftDal();

        #region 前台加载软件

        /// <summary>
        /// 前台加载软件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Soft PublicShow(int id)
        {
            Soft model = MySoftDAL.PublicShow(id);


            return model;
        }

        #endregion

        #region 下载软件

        /// <summary>
        /// 下载软件
        /// </summary>
        /// <param name="softId"></param>
        /// <param name="downListId"></param>
        public void DownloadThisSoft(int softId, int downListId)
        {
            Soft soft = MySoftDAL.PublicShow(softId);

            if (soft == null)
            {
                HttpContext.Current.Response.Write("该软件不存在或者被管理员取消审核。");
                return;
            }
            //如果是免费软件直接给出下载地址。
            if (soft.Rank < 1)
            {
                //增加下载次数
                new CoreDal().UpDateHits(softId, true);
                GetDownloadUrl(1, softId, downListId);
                return;
            }
            /////////////////////////执行会员下载/////////////////////////////
            if (RightsCheck.IsLogined == false)
            {
                HttpContext.Current.Response.Write("请登录。");
                return;
            }

            User m_model = new UserManage().GetInfo(UserManage.Current.UserID);

            if (UserManage.Current.ActiveType == 2)
            {
                if (m_model.Mature < DateTime.Today)
                {
                    HttpContext.Current.Response.Write("对不起，你的会员期已经过了。");
                    return;
                }
            }

            int intPoint = m_model.Point;
            int intCoin = m_model.Coin;

            if (intPoint < soft.PointNum)
            {
                if (intCoin > 0)
                {
                    CMSConfig config = CMSConfig.Instance();
                    HttpContext.Current.Response.Write("对不起，你的点数不够，但你还有" + intCoin + "个" + config.Member.JingBiMingCheng +
                                                       "请把它转化为点数。");
                    return;
                }

                HttpContext.Current.Response.Write("对不起，你的点数不够。");
                return;
            }

            //如果权限足够
            if (soft.Rank <= UserManage.Current.Rank)
            {
                //如果不是包月
                if (UserManage.Current.ActiveType != 2)
                {
                    //如果没有免费下软件的待遇
                    if (RightsCheck.HasRight("Member_1") == false)
                    {
                        //如果没有下载过                       
                        BusinessDal myBusinessCategory = new BusinessDal();
                        DateTime? businessReaded = myBusinessCategory.CheckReaded(softId, UserManage.Current.UserID);

                        Business model = new Business
                        {
                            Flag = 2,
                            CreateTime = DateTime.Now,
                            Customer = UserManage.Current.UserName,
                            Point = soft.PointNum,
                            PayMoney = 0,
                            Indent = softId,
                            UserId = UserManage.Current.UserID,
                            Remark = "下软件扣点数"
                        };
                        if (businessReaded == null)
                        {
                            //下软件扣点数
                            new MemberManage().KouDianShu(soft.PointNum, UserManage.Current.UserID);
                            new BusinessManage().Add(model);
                        }
                        else
                        {
                            //增加下载次数
                            new CoreDal().UpDateHits(softId, true);

                            double dec = MyDateTime.DateDiff(DateDiffOption.Day, businessReaded, DateTime.Now);
                            if (dec > 0)
                            {
                                //下软件扣点数
                                new MemberManage().KouDianShu(soft.PointNum, UserManage.Current.UserID);

                                new BusinessManage().Add(model);
                            }
                        }
                    }
                }
                GetDownloadUrl(1, softId, downListId);
            }
            else
            {
                HttpContext.Current.Response.Write("对不起，你的等级不够。");
            }
        }


        /// <summary>
        /// 获取下载链接
        /// </summary>
        /// <param name="ModelId"></param>
        /// <param name="SoftID"></param>
        /// <param name="DownListID"></param>
        private void GetDownloadUrl(byte ModelId, int SoftID, int DownListID)
        {
            IList<SoftDownList> list = new SoftDownListDal().GetDownList(ModelId, SoftID);
            var info = list.Where(x => x.Id == DownListID).FirstOrDefault();
            if (info == null)
            {
                HttpContext.Current.Response.Write("对不起，找不到下载地址。");
            }
            else
            {
                HttpContext.Current.Response.Redirect(info.FileName); //软件的路径
            }
        }

        #endregion
    }
}